.PHONY: debug run migrate init-data release feature_test help
.DEFAULT_GOAL := help

define BROWSER_PYSCRIPT
import os, webbrowser, sys
try:
	from urllib import pathname2url
except:
	from urllib.request import pathname2url

webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1])))
endef
export BROWSER_PYSCRIPT

define PRINT_HELP_PYSCRIPT
import re, sys

for line in sys.stdin:
	match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line)
	if match:
		target, help = match.groups()
		print("%-20s %s" % (target, help))
endef
export PRINT_HELP_PYSCRIPT

BROWSER := python -c "$$BROWSER_PYSCRIPT"

help:
	@python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST)

debug: ## 以Debug模式运行
	python ./main.py

run: ## 以Prod模式运行
	uvicorn main:app --reload

migrate: ## 升级数据库结构
	alembic revision --autogenerate -m "up"&&\
	alembic upgrade head

init-data: ## 初始化内置数据
	python ./initial_data.py

release: ## 发布MegaIde程序
	mkdir -p ./public&&\
	docker run --rm -v `pwd`:/codes -i node:16.13.0 sh /codes/build-web.sh &&\
	docker build -t mega-ide .

#unit_test:  ##
	#python ./tests/unit_tests/test_main.py

feature_test: ##实现场景测试
	rm -Rf ./htmlcov
	rm -Rf ./.coverage
	coverage run -a -m --source='./server' behave
	coverage html